package edu.johnpank.libassistant.services

import edu.johnpank.libassistant.entity.Book
import edu.johnpank.libassistant.repo.BookRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

@Service
class BookService(
    @Autowired
    private val bookRepository: BookRepository
) {

    fun getAllBook(): MutableList<Book>{
        return bookRepository.findAll() as MutableList<Book>
            }

    fun getBookById(id: Long): Book {
        return bookRepository.findById(id).get()
    }

    fun getBookByIsbn(isbn: String): Book {
        return bookRepository.findByIsbn(isbn).get()
    }

    fun findBookByContainedString(string: String): MutableList<Book>{
        return bookRepository.findByAuthorContainingIgnoreCaseOrTitleContainingIgnoreCaseOrSeriesContainingIgnoreCaseOrGenreContainingIgnoreCase(string, string, string, string) as MutableList<Book>
    }

    @Transactional
    fun addBook(book: Book){
        bookRepository.save(book)
    }

    @Transactional
    fun updateBook(book: Book){
        bookRepository.save(book)
    }

    @Transactional
    fun deleteBook(book: Book){
        bookRepository.delete(book)
    }
}